/*
 * locore.s for ERC32
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 675
 * Mass Ave, Cambridge, MA 02139, USA.
 *
 */


/*
 * locore.s for ERC32/LEON
 */
 
/* The traptable has to be the first code in a boot PROM. */

#define	TRAP(H)  mov %psr, %l0; sethi %hi(H), %l4; jmp %l4+%lo(H); nop;
#define	TRAPL(H) mov %g0, %l0; sethi %hi(H), %l4; jmp %l4+%lo(H); nop;

/* Unexcpected trap will halt the processor by forcing it to error state */
#define BAD_TRAP ta 0; nop; nop; nop;

/* Software trap. Treat as BAD_TRAP for the time being... */
#define SOFT_TRAP BAD_TRAP

#define PSR_INIT   0x10c0	/* Disable traps, set s and ps */
#define WIM_INIT   2

WINDOWSIZE = (16 * 4)
ARGPUSHSIZE = (6 * 4)
ARGPUSH = (WINDOWSIZE + 4)
MINFRAME = (WINDOWSIZE + ARGPUSHSIZE + 4)
#define STACK_ALIGN	8
#define SA(X)	(((X)+(STACK_ALIGN-1)) & ~(STACK_ALIGN-1))

  .section	".text"
  .align	4
  .global _trap_table, start, _start, _main, main, irq_uart, irq_gptimer, irq_gpio, irq_int0

  /* Hardware traps */
start:
#	jmp 0x10;
#	nop;
#	nop;
#	nop;

_trap_table:
  TRAPL(reset);			! 00 reset trap 
  BAD_TRAP;			! 01 instruction_access_exception
  BAD_TRAP;			! 02 illegal_instruction
  BAD_TRAP;			! 03 priveleged_instruction
  TRAP(_fpdis);			! 04 fp_disabled
  TRAP(_window_overflow);	! 05 window_overflow
  TRAP(_window_underflow);	! 06 window_underflow
  BAD_TRAP;			! 07 memory_address_not_aligned
  BAD_TRAP;			! 08 fp_exception
  BAD_TRAP;			! 09 data_access_exception
  BAD_TRAP;			! 0A tag_overflow

  BAD_TRAP;			! 0B undefined
  BAD_TRAP;			! 0C undefined
  BAD_TRAP;			! 0D undefined
  BAD_TRAP;			! 0E undefined
  BAD_TRAP;			! 0F undefined
  BAD_TRAP;			! 10 undefined

  /* Interrupt entries */
  TRAP(irqtrap);		! 11 interrupt level 1
  TRAP(irqtrap);		! 12 interrupt level 2
  TRAP(irqgpio);		! 13 gpio interrupt level 3
  TRAP(irqint0);		! 14 interrupt level 4
  TRAP(irqtrap);		! 15 interrupt level 5
  TRAP(irqtrap);		! 16 interrupt level 6
  TRAP(irquart);		! 17 uart interrupt level 7
  TRAP(irqgptimer);	! 18 gptimer interrupt level 8
  TRAP(irqtrap);		! 19 interrupt level 9
  TRAP(irqtrap);		! 1A interrupt level 10
  TRAP(irqtrap);		! 1B interrupt level 11
  TRAP(irqtrap);    ! 1C interrupt level 12
  TRAP(irqtrap);		! 1D interrupt level 13
  TRAP(irqtrap);		! 1E interrupt level 14
  TRAP(irqtrap);		! 1F interrupt level 15
  /*
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 20 - 23 undefined
  BAD_TRAP;					! 24 cp_disabled
  BAD_TRAP; BAD_TRAP; BAD_TRAP;			! 25 - 27 undefined
  BAD_TRAP;					! 28 cp_exception
  BAD_TRAP; BAD_TRAP; BAD_TRAP;			! 29 - 2B undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 2C - 2F undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 30 - 33 undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 34 - 37 undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 38 - 3B undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 3C - 3F undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 40 - 43 undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 44 - 47 undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 48 - 4B undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 4C - 4F undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 50 - 53 undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 54 - 57 undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 58 - 5B undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 5C - 5F undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 60 - 63 undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 64 - 67 undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 68 - 6B undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 6C - 6F undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 70 - 73 undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 74 - 77 undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 78 - 7B undefined
  BAD_TRAP; BAD_TRAP; BAD_TRAP; BAD_TRAP;	! 7C - 7F undefined
*/
  /* Software traps */
  /*
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; 		! 80 - 82
  SOFT_TRAP; !(_flush_windows)			! 83
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! 84 - 87
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! 88 - 8B
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! 8C - 8F
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! 90 - 93
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! 94 - 97
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! 98 - 9B
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! 9C - 9F
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! A0 - A3
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! A4 - A7
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! A8 - AB
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! AC - AF
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! B0 - B3
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! B4 - B7
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! B8 - BB
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! BC - BF
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! C0 - C3
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! C4 - C7
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! C8 - CB
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! CC - CF
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! D0 - D3
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! D4 - D7
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! D8 - DB
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! DC - DF
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! E0 - E3
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! E4 - E7
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! E8 - EB
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! EC - EF
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! F0 - F3
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! F4 - F7
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! F8 - FB 
  SOFT_TRAP; SOFT_TRAP; SOFT_TRAP; SOFT_TRAP;	! FC - FF
*/

reset:
	flush
	set 0xc0, %g1		! init IU
	mov %g1, %psr
	mov %g0, %wim
	mov %g0, %y
	nop
	set  0x01000f, %g1
	!set  0x81000f, %g1
	sta %g1, [%g0] 2        ! init cache control reg
	
	!added by wangyd
	set     0x00000000, %g1       	! Initialize TBR
	mov     %g1, %tbr

        !!!!!!!!!!!!!!!!!!dsu register configuration!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        
        set 0x0000000F, %g1   
        set 0x90000000, %g7      ! %g7=dsu register original address        
        st %g1, [%g7 + 0x000000] ! DSU control register
        set 0, %g1
        st %g1, [%g7 + 0x000020] ! break and single step register
        set 0, %g1
        st %g1, [%g7 + 0x000024] ! debug mode mask register
        set 0x00000001, %g1
        st %g1, [%g7 + 0x000040] ! AHB trace buffer control register 
        set 0x02000000, %g1
        st %g1, [%g7 + 0x000044] ! AHB trace buffer index register
        set 0, %g1
        set 0x90110000, %g7
        st %g1, [%g7] ! Instruction trace control register
         
        

        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
        
	set  0x80000000, %g7	! %g7=On chip registers original address
	!set  0x00000200, %g1	! MCFG1
	!st %g1, [%g7 + 0x00]
	!set  0x00001E30, %g1	! MCFG2
	!st %g1, [%g7 + 0x04]
	!!set  0x5001000F, %g1	! Cache control register
	!!st %g1, [%g7 + 0x14]
	set  0, %g1
	st %g1, [%g7 + 0x200]	! Interrupt level register
	set  0, %g1
	st %g1, [%g7 + 0x208]	! Interrupt force register
	set  0x2000, %g3
	st %g3, [%g7 + 0x240]	! Interrupt mask register
	set  0, %g1
	st %g1, [%g7 + 0x204]	! Interrupt pending register
	set  -1, %g3
	st %g3, [%g7 + 0x20c]	! Interrupt clear register
	set  0, %g1
	st %g1, [%g7 + 0x504]	! I/O port input/output register
	set  0xff, %g1
	st %g1, [%g7 + 0x508]	! I/O port direction register
	set  0, %g1
	st %g1, [%g7 + 0x50C]	! I/O port interrupt mask register
        !set  0x00000001, %g1
        !st %g1, [%g7 + 0x408]    ! uart control register 

      


	!set  0x10, %g1
	!st %g1, [%g7 + 0x40]	! Timer 1 counter register
	!set  0x10, %g1
	!st %g1, [%g7 + 0x44]	! Timer 1 reload register
	!set  0, %g1
	!st %g1, [%g7 + 0x48]	! Timer 1 control register
	!set  0, %g1
	!st %g1, [%g7 + 0x50]	! Timer 2 counter register
	!set  0x8, %g1
	!st %g1, [%g7 + 0x54]	! Timer 2 reload register
	!set  0, %g1
	!st %g1, [%g7 + 0x58]	! Timer 2 control register
	!set  0x100, %g1	
	!st %g1, [%g7 + 0x60]	! Prescaler counter register
	!set  0x100, %g1	
	!st %g1, [%g7 + 0x64]	! Prescaler reload register
	mov	0x01, %g1
	mov	%g1, %wim
	set	0xe0, %g1	! enable traps
	mov 	%g1, %psr
	nop
	call	_initregs
	nop
	set 0x40003f00, %g3 !match with sh.x stack
	mov	%g3, %fp
	sub	%g3, 0x100, %sp
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!added by wangyd	
	set 0x40000000, %g1
	st	%sp, [ %g1 ]
	nop
	nop
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	call main		! call main routine
        nop
!	unimp			! for test only


	.global	_fpdis	
_fpdis:
	set	0x1000, %l4
	andcc	%l0, %l4, %l3
	bne,a	1f
	andn	%l0, %l4, %l0
	ta	0
1:
	mov      %l0, %psr		! restore %psr
	nop; nop; nop
  	jmp  %l2			! Jump to nPC
  	rett  %l2 + 4
  	
  	
  	.global _irqtbl
irqtrap:
	! store trap type in irqtbl for later verification
	set	_irqtbl, %l4
	ldub	[%l4], %l5
	srl	%l3, 4, %l3
	and	%l3, 0x0ff, %l3
	stb	%l3, [%l4+%l5]
	add	%l5, 1, %l5
	stb	%l5, [%l4]
	jmpl	%l1, %g0
	rett	%l2
	nop
	nop

irqgptimer:
	! save icc -> %l0
  	set 0x00f00000, %l3
  	and %l0, %l3, %l0

  	! enable trap and set pil to block other external interrupt

  	! store g1 - g7
  	st  %g1, [ %fp + -4 ]
  	st  %g2, [ %fp + -8 ]
  	st  %g3, [ %fp + -12 ]
  	st  %g4, [ %fp + -16 ]
  	st  %g5, [ %fp + -20 ]
  	st  %g6, [ %fp + -24 ]
  	st  %g7, [ %fp + -28 ]

  	! call interrupt service routine
  	call irq_gptimer
  	add %fp, -32, %sp

  	! load g1 - g7
  	ld   [ %fp + -4 ] ,  %g1
  	ld   [ %fp + -8 ] ,  %g2
  	ld   [ %fp + -12 ] ,  %g3
  	ld   [ %fp + -16 ],  %g4
  	ld   [ %fp + -20 ],  %g5
  	ld   [ %fp + -24 ],  %g6
  	ld   [ %fp + -28 ],  %g7

  	! restore icc
  	rd %psr, %l7
  	set 0xff0fffff, %l3
  	and %l7, %l3, %l7
  	wr %l0, %l7, %psr

  	! disable trap and clear pil

  	nop
  	nop
  	nop
  	! return
  	jmpl	%l1, %g0
  	rett      %l2;

irquart:
	! save icc -> %l0
  	set 0x00f00000, %l3
  	and %l0, %l3, %l0

  	! enable trap and set pil to block other external interrupt

  	! store g1 - g7
  	st  %g1, [ %fp + -4 ]
  	st  %g2, [ %fp + -8 ]
  	st  %g3, [ %fp + -12 ]
  	st  %g4, [ %fp + -16 ]
  	st  %g5, [ %fp + -20 ]
  	st  %g6, [ %fp + -24 ]
  	st  %g7, [ %fp + -28 ]

  	! call interrupt service routine
  	call irq_uart
  	add %fp, -32, %sp

  	! load g1 - g7
  	ld   [ %fp + -4 ] ,  %g1
  	ld   [ %fp + -8 ] ,  %g2
  	ld   [ %fp + -12 ] ,  %g3
  	ld   [ %fp + -16 ],  %g4
  	ld   [ %fp + -20 ],  %g5
  	ld   [ %fp + -24 ],  %g6
  	ld   [ %fp + -28 ],  %g7

  	! restore icc
  	rd %psr, %l7
  	set 0xff0fffff, %l3
  	and %l7, %l3, %l7
  	wr %l0, %l7, %psr

  	! disable trap and clear pil

  	nop
  	nop
  	nop
  	! return
  	jmpl	%l1, %g0
  	rett      %l2;

irqgpio:
	! save icc -> %l0
  	set 0x00f00000, %l3
  	and %l0, %l3, %l0

  	! enable trap and set pil to block other external interrupt

  	! store g1 - g7
  	st  %g1, [ %fp + -4 ]
  	st  %g2, [ %fp + -8 ]
  	st  %g3, [ %fp + -12 ]
  	st  %g4, [ %fp + -16 ]
  	st  %g5, [ %fp + -20 ]
  	st  %g6, [ %fp + -24 ]
  	st  %g7, [ %fp + -28 ]

  	! call interrupt service routine
  	call irq_gpio
  	add %fp, -32, %sp

  	! load g1 - g7
  	ld   [ %fp + -4 ] ,  %g1
  	ld   [ %fp + -8 ] ,  %g2
  	ld   [ %fp + -12 ] ,  %g3
  	ld   [ %fp + -16 ],  %g4
  	ld   [ %fp + -20 ],  %g5
  	ld   [ %fp + -24 ],  %g6
  	ld   [ %fp + -28 ],  %g7

  	! restore icc
  	rd %psr, %l7
  	set 0xff0fffff, %l3
  	and %l7, %l3, %l7
  	wr %l0, %l7, %psr

  	! disable trap and clear pil

  	nop
  	nop
  	nop
  	! return
  	jmpl	%l1, %g0
  	rett      %l2;


irqint0:
	! save icc -> %l0
  	set 0x00f00000, %l3
  	and %l0, %l3, %l0

  	! enable trap and set pil to block other external interrupt

  	! store g1 - g7
  	st  %g1, [ %fp + -4 ]
  	st  %g2, [ %fp + -8 ]
  	st  %g3, [ %fp + -12 ]
  	st  %g4, [ %fp + -16 ]
  	st  %g5, [ %fp + -20 ]
  	st  %g6, [ %fp + -24 ]
  	st  %g7, [ %fp + -28 ]

  	! call interrupt service routine
  	call irq_int0
  	add %fp, -32, %sp

  	! load g1 - g7
  	ld   [ %fp + -4 ] ,  %g1
  	ld   [ %fp + -8 ] ,  %g2
  	ld   [ %fp + -12 ] ,  %g3
  	ld   [ %fp + -16 ],  %g4
  	ld   [ %fp + -20 ],  %g5
  	ld   [ %fp + -24 ],  %g6
  	ld   [ %fp + -28 ],  %g7

  	! restore icc
  	rd %psr, %l7
  	set 0xff0fffff, %l3
  	and %l7, %l3, %l7
  	wr %l0, %l7, %psr

  	! disable trap and clear pil

  	nop
  	nop
  	nop
  	! return
  	jmpl	%l1, %g0
  	rett      %l2;
  	  	

_skip:
	jmpl	%l2, %g0
	rett	%l2 + 4
	nop
	nop

_skipn:
	set	_dexcn, %l4
	ld	[%l4], %l5
	subcc	%l5, 1, %l5
	st	%l5, [%l4]
	bl	dexcfail
	nop
	jmpl	%l2, %g0
	rett	%l2 + 4
_reexn:
	set	_dexcn, %l4
	ld	[%l4], %l5
	subcc	%l5, 1, %l5
	st	%l5, [%l4]
	bl	dexcfail
	nop
	jmpl	%l1, %g0
	rett	%l2
dexcfail:
	ta	0
	nop
	nop

halt:	ta	0

/* Number of register windows */

  	 		!Window overflow trap handler.
  	.global _window_overflow

_window_overflow:

  	mov  %wim, %l3       		! Calculate next WIM
  	mov  %g1, %l7
  	srl  %l3, 1, %g1
  	sll  %l3, 14 , %l4
  	or   %l4, %g1, %g1

  	save                              ! Get into window to be saved.
  	mov  %g1, %wim
  	nop; nop; nop
        st   %l0, [%sp + 0];
        st   %l1, [%sp + 4];
        st   %l2, [%sp + 8];
        st   %l3, [%sp + 12];
        st   %l4, [%sp + 16];
        st   %l5, [%sp + 20];
        st   %l6, [%sp + 24];
        st   %l7, [%sp + 28];
        st   %i0, [%sp + 32];
        st   %i1, [%sp + 36];
        st   %i2, [%sp + 40];
        st   %i3, [%sp + 44];
        st   %i4, [%sp + 48];
        st   %i5, [%sp + 52];
        st   %i6, [%sp + 56];
        st   %i7, [%sp + 60];
  	restore				! Go back to trap window.
  	mov  %l7, %g1
  	jmp  %l1			! Re-execute save.
  	rett %l2

  /* Window underflow trap handler.  */

  	.global  _window_underflow

_window_underflow:

	mov  %wim, %l3			! Calculate next WIM
	sll  %l3, 1, %l4
  	srl  %l3, 14, %l5
  	or   %l5, %l4, %l5
  	mov  %l5, %wim
  	nop; nop; nop
  	restore				! Two restores to get into the
  	restore				! window to restore
        ld   [%sp + 0], %l0; 		! Restore window from the stack
        ld   [%sp + 4], %l1;
        ld   [%sp + 8], %l2;
        ld   [%sp + 12], %l3;
        ld   [%sp + 16], %l4;
        ld   [%sp + 20], %l5;
        ld   [%sp + 24], %l6;
        ld   [%sp + 28], %l7;
        ld   [%sp + 32], %i0;
        ld   [%sp + 36], %i1;
        ld   [%sp + 40], %i2;
        ld   [%sp + 44], %i3;
        ld   [%sp + 48], %i4;
        ld   [%sp + 52], %i5;
        ld   [%sp + 56], %i6;
        ld   [%sp + 60], %i7;
  	save				! Get back to the trap window.
  	save
  	jmp  %l1			! Re-execute restore.
  	rett  %l2

	.global	_flush_windows
!_flush_windows:

	!mov	%psr, %g1
	!or	%g1, 0x0f00, %g2
	!restore				! enter previous frame (cannot trap)
        !wr      %g2, 0x20, %psr		! enable traps, disable interrupts
	!nop; nop; nop
	!save				! 6 save to flush all windows
	!save
	!save
	!save
	!save
	!save
	!restore				! 5 restore to enter trapped frame
	!restore
	!restore
	!restore
	!restore
	!mov      %g1, %psr		! restore %psr
	!nop; nop; nop
  	!jmp  %l2			! Jump to nPC
  	!rett  %l2 + 4

	.text
	.align 16
	.global _initregs
	.proc	04

_initregs:
	!mov	%g0, %g1
	mov	%o7, %g2
	!mov	%g0, %g3
	mov	%g0, %g4
	mov	%g0, %g5
	mov	%g0, %g6
	mov	%g0, %g7
	mov	%wim, %g7
	mov	%g0, %wim
	nop; nop; nop
	clr	%g1
1:
	mov	%g0, %l0
	mov	%g0, %l1
	mov	%g0, %l2
	mov	%g0, %l3
	mov	%g0, %l4
	mov	%g0, %l5
	mov	%g0, %l6
	mov	%g0, %l7
	mov	%g0, %o0
	mov	%g0, %o1
	mov	%g0, %o2
	mov	%g0, %o3
	mov	%g0, %o4
	mov	%g0, %o5
	mov	%g0, %o6
	mov	%g0, %o7
	save
	subcc	%g1, 14, %g0 !nwin-1
	bne	1b
	inc	%g1
        nop
        nop
	mov	%g7, %wim
	nop
	nop
	nop
	mov	%g2, %o7
	retl
	nop

	.global _setpil
_setpil:
	retl
	ta	2

spil:
	mov	%psr, %l4
	srl	%l4, 8, %l5
	andn	%l4, 0x0f00, %l4
	sll	%i0, 8, %i0
	and	%i0, 0x0f00, %i0
	or	%i0, %l4, %l4
	mov	%l4, %psr
	and	%l5, 0x0f, %i0
	jmpl	%l2, %g0
	rett	%l2 + 4

	.section	".bss"
	.global _dexcn, dexcn, _irqtbl, irqtbl
_dexcn:
dexcn:
	.word 0
_irqtbl:
irqtbl:
	.word 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
